package com.ly.nio;
import java.net.SocketAddress;
import	java.nio.file.StandardOpenOption;
import	java.nio.channels.ServerSocketChannel;
import	java.nio.channels.FileChannel;
import	java.nio.ByteBuffer;
import java.io.IOException;
import java.net.InetSocketAddress;
import	java.nio.channels.SocketChannel;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;

/**
 * @Classname NioBlock
 * @Description
 *
 *  SocketChannel
 *  ServerSocketChannel
 *  DatagramChannel
 *
 *
 * @Date 2020/9/30 19:10
 * @Author 冷心影翼
 */
public class NioBlock {

	public static void main(String[] args) throws IOException {
//		server();
//		client();
//		server2();
//		client2();
	}

	public static void client() throws IOException {
		SocketChannel channel = SocketChannel.open(new InetSocketAddress("127.0.0.1.txt",8848));
		ByteBuffer buffer = ByteBuffer.allocate(1024);
		FileChannel inChannel = FileChannel.open(Paths.get("src/nio/test.txt"), StandardOpenOption.READ);

		while (inChannel.read(buffer)!=-1) {
			buffer.flip();
			channel.write(buffer);
			buffer.clear();
		}

		channel.close();
		inChannel.close();
	}

	public static void server() throws IOException {
		ServerSocketChannel channel = ServerSocketChannel.open();
		FileChannel fileChannel = FileChannel.open(Paths.get("src/nio/c.txt"),StandardOpenOption.WRITE,StandardOpenOption.CREATE);
		channel.bind(new InetSocketAddress(8848));
		SocketChannel accept = channel.accept();

		ByteBuffer buffer = ByteBuffer.allocate(1024);
		while (accept.read(buffer)!=-1) {
			buffer.flip();
			fileChannel.write(buffer);
			buffer.clear();
		}

		accept.close();
		channel.close();
		fileChannel.close();
	}

	public static void client2() throws IOException {
		SocketChannel channel = SocketChannel.open(new InetSocketAddress("127.0.0.1.txt",8848));
		FileChannel fileChannel = FileChannel.open(Paths.get("src/nio/test.txt"),StandardOpenOption.READ);
		ByteBuffer byteBuffer = ByteBuffer.allocate(1024);

		while (fileChannel.read(byteBuffer)!=-1) {
			byteBuffer.flip();
			channel.write(byteBuffer);
			byteBuffer.clear();
		}

		channel.shutdownOutput();
		//接收服务端反馈
		int lenth = 0;
		while ( (lenth = channel.read(byteBuffer)) > 0 ) {
			byteBuffer.flip();
			System.out.println(new String(byteBuffer.array(),0,lenth));
			byteBuffer.clear();
		}
		fileChannel.close();
		channel.close();
	}

	public static void server2() throws IOException {
	    ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
		serverSocketChannel.bind(new InetSocketAddress(8848));
		SocketChannel accept = serverSocketChannel.accept();
		FileChannel fileChannel = FileChannel.open(Paths.get("src/nio/d.txt"),StandardOpenOption.WRITE,StandardOpenOption.CREATE);
		ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
		while (accept.read(byteBuffer)!=-1) {
			byteBuffer.flip();
			fileChannel.write(byteBuffer);
			byteBuffer.clear();
		}

		//反馈成功
		byteBuffer.put("接收成功".getBytes());
		byteBuffer.flip();

		accept.write(byteBuffer);

		serverSocketChannel.close();
		accept.close();
		fileChannel.close();
	}


}
